<script setup>
import { Edit, Delete } from '@element-plus/icons-vue'
import { ref } from 'vue'
import ArticleCateName from './components/ArticleCateName.vue'
import { articleGetListService } from '@/api/article'
import { formatTime } from '@/utils/format'
import ArticleEdit from '@/views/article/components/ArticleEdit.vue'

const articleList = ref([])
const total = ref(0)
const loading = ref(false)
const articleEditRef = ref()
// 定义查询参数
const params = ref({
    pagenum: 1,
    pagesize: 5,
    cate_id: '',
    state: ''
})
const getArticleList = async () => {
    loading.value = true
    const res = await articleGetListService(params.value)
    // console.log(res.data)
    articleList.value = res.data.data
    // total.value = res.data.total
    total.value = res.data.total
    loading.value = false
}
getArticleList()

// 处理分页逻辑
const onSizeChange = (size) => {
    params.value.pagenum = 1
    params.value.pagesize = size
    getArticleList()
}
const onCurrentChange = (page) => {
    params.value.pagenum = page
    getArticleList()
}

const onSearch = () => {
    params.value.pagenum = 1
    getArticleList()
}
const onReset = () => {
    params.value.pagenum = 1
    params.value.cate_id = ''
    params.value.state = ''
    getArticleList()
}

const onAddArticle = () => {
    articleEditRef.value.open({})
}

const onEdit = (row) => {
    // console.log(row)
    articleEditRef.value.open(row)
}

const onDelete = (row) => {
    console.log(row)
}
const onSuccess = (type) => {
    if (type === 'add') {
        // 如果添加操作，最好渲染最后一页
        const lastPage = Math.ceil((total.value + 1) / params.value.pagesize)
        params.value.pagenum = lastPage
    }
    getArticleList()
}
</script>
<template>
    <page-container title="文章管理">
        <template #extra>
            <el-button type="primary" @click="onAddArticle">发布文章</el-button>
        </template>
        <!-- 表单区域 -->
        <el-form inline>
            <article-cate-name width="200px" v-model="params.cate_id"></article-cate-name>
            <el-form-item label="发布状态">
                <el-select v-model="params.state" style="width: 200px;">
                    <el-option label="已发布" value="已发布"></el-option>
                    <el-option label="草稿" value="草稿"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="onSearch">搜索</el-button>
                <el-button @click="onReset">重置</el-button>
            </el-form-item>
        </el-form>
        <!-- 表格区域 -->
        <el-table :data="articleList" v-loading="loading">
            <el-table-column prop="title" label="文章标题">
                <template #default="{ row }">
                    <el-link type="primary">{{ row.title }}</el-link>
                </template>
            </el-table-column>
            <el-table-column prop="cate_name" label="分类"></el-table-column>
            <el-table-column prop="pub_date" label="发表时间">
                <template #default="{ row }">
                    {{ formatTime(row.pub_data) }}
                </template>
            </el-table-column>
            <el-table-column prop="state" label="状态"></el-table-column>
            <el-table-column label="操作">
                <template #default="{ row }">
                    <el-button circle plain :icon="Edit" type="primary" @click="onEdit(row)"></el-button>
                    <el-button circle plain :icon="Delete" type="danger" @click="onDelete(row)"></el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- 分页区域 -->
        <el-pagination
            v-model:current-page="params.pagenum"
            v-model:page-size="params.pagesize"
            :page-sizes="[2, 3, 4, 5]"
            :background="true"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="onSizeChange"
            @current-change="onCurrentChange"
            style="margin-top: 20px; justify-content: flex-end;"
        />
        <!-- 抽屉弹窗 -->
        <article-edit ref="articleEditRef" @success="onSuccess"></article-edit>
    </page-container>
</template>
<style lang="scss" scoped>

</style>